<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8"%>
<%@ include file="../include/laypublic.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <title>工作流配置节点</title>
</head>
<style>
	.layui-btn-red {
	    border: 1px dashed #FF0000;
	    background-color: #fff;
	    color: #555;
	}
	.layui-btn-red:hover{border-color:#FF0000;color:#333}
	.fieldset{
		margin-top: 5px;
		margin-bottom: 0px
	}
	.layui-legend{
		background-color:#5fb878;
		font-size:15px !important;
		width:140px;
	}
	.layui-form-item {
	    margin-bottom: 5px;
	    clear: both;
	    *zoom: 1;
	}
	.layui-form-label{
		width: 120px;
    	text-align: left;
    	margin-left: 90px;
    }
    .icon-i{
    	position: absolute;
    	top:12px;
    	right: 8px;
    }
	.circle{
		width: 70px;
    	height: 70px;
    	line-height: 70px;
    	margin: 0 auto 10px;
    	text-align: center;
    	background-color: #009688;
    	cursor: pointer;
    	color: #fff;
    	border-radius: 50%;
    	padding: unset;
	}
	.marginset{
		margin:20px 0;
	}
	.width122{
		width: 200px;
		cursor: unset;
	}
	.layui-input-block {
		margin-left: 10px;
		margin-right: 10px;
		margin-bottom: 10px;
	}
	.layui-icon-search {
		cursor: pointer;
	}
	.margin-top5 {
		margin-top: 5px;
	}

</style>
	<script type="text/html" id="node_div">
	<div style="border:1px solid #5fb878;" id="nodeDiv{0}_{1}">
		<input type="hidden" value="{0}" name="nodeIdx{0}_{1}" />
		<input type="hidden"  name="nodePolicy{0}_{1}" />
		<input type="hidden" name="nodeName{0}_{1}">
		<div class="layui-input-block width122 margin-top5">
			<div class="layui-inline" style="width: 130px;" onclick="stopPror(event)">
				<select name="routeFlag{0}_{1}" lay-verify="required" lay-filter="add_select_routeFlag">
					<option value="">请选择-审批角色范围</option>
					<option value="1" selected="selected">集团总部</option>
					<option value="2">区域公司</option>
					<option value="3">项目公司</option>
					<option value="4">本部门内</option>
				</select>
			</div>
			<div class="layui-inline">
				<button type="button" class="layui-btn layui-btn-normal layui-btn-sm"  id="deleteNodeDiv{0}_{1}" onclick="deleteDiv('{0}_{1}',event)"><i class="layui-icon">&#xe640;</i></button>
			</div>
		</div>
		<div class="layui-input-block width122 margin-top5">
			<input style="display:block" type="text" id="checkUserName{0}_{1}" readonly="readonly" style="border:0px" autocomplete="off" placeholder="请选择审批角色组" class="layui-input">
			<i style="display:block" class="layui-icon layui-icon-search icon-i" id="queryCheck{0}_{1}" onclick="queryCheckUser('{0}_{1}',0);"></i>
			<input style="display:none" type="hidden" name="chooseKey{0}_{1}" id="chooseKey{0}_{1}" maxlength="50"  autocomplete="off" placeholder="请输入对应名称" class="layui-input"/>
		</div>
	</div>
</script>

<script type="text/html" id="layui-row">
	<div class="layui-inline" id="linkRow{0}" lay-filter="linkRow{0}" >
		<i class="layui-icon layui-icon-right"></i>
		<div class="layui-inline">
			<input type="text" name="nodeTitle{0}"  onchange="inputchange({0},this)" class="layui-input width122" style="border: none;background-color: #009688;color: #fff" onclick="stopPror(event)">
		</div>

		<div style="border:1px solid #5fb878" id="nodeDiv{0}_1">
			<div class="layui-inline">
				<input type="text" name="nodePolicyName{0}" disabled class="layui-input layui-disabled " style="width:120px;border: none">
			</div>
			<div class="layui-inline">
				<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="addLinkBtn{0}" style="display: none" onclick="addRow2({0},event);"><i class="layui-icon">&#xe654;</i></button><%--onclick="addRow(this);"--%>
				<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="delLinkBtn{0}" onclick="deleteRow({0},event)"><i class="layui-icon">&#xe640;</i></button>
			</div>
			<input type="hidden" value="{0}" name="nodeIdx{0}_1" />
			<input type="hidden"  name="nodePolicy{0}_1" />
			<input type="hidden" name="nodeName{0}_1">
			<div class="layui-input-block width122 margin-top5">
				<div class="layui-inline" style="width: 130px;" onclick="stopPror(event)">
					<select name="routeFlag{0}_1" lay-verify="required" lay-filter="add_select_routeFlag">
						<option value="">请选择-审批角色范围</option>
						<option value="1" selected="selected">集团总部</option>
						<option value="2">区域公司</option>
						<option value="3">项目公司</option>
						<option value="4">部门负责人</option>
						<option value="5">公司法人</option>
					</select>
				</div>
				<div class="layui-inline">
					<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="deleteNodeDiv{0}_1" onclick="deleteDiv('{0}_1',event)"><i class="layui-icon">&#xe640;</i></button>
				</div>

			</div>
			<div class="layui-input-block width122 margin-top5">
				<input style="display:block" type="text" id="checkUserName{0}_1" readonly="readonly" style="border:0px" autocomplete="off" placeholder="请选择审批角色" class="layui-input">
				<i style="display:block" class="layui-icon layui-icon-search icon-i" id="queryCheck{0}_1" onclick="queryCheckUser('{0}_1',0);"></i>
				<input style="display:none" type="hidden" name="chooseKey{0}_1" id="chooseKey{0}_1" maxlength="50"  autocomplete="off" placeholder="请输入对应名称" class="layui-input"/>
			</div>
			<%--<div style="border:1px dashed #d0d0d0;margin: 3px">

			</div>--%>

		</div>

	</div>

</script>
<%--<script id="myMenus" type="text/plain">
[
  [{txt: "个人中心" + (5*2), event: "usercenter"}]
  [{txt: "详细信息" + Math.random(), event: "detail"}]
  [{txt: "退出登录" , event: "logout"}]
]
</script>--%>
<body>
<div class="layui-fluid">
    <div class="layui-card">
	    <div class="layui-tab-item layui-show">
			<div style="padding-top:15px;">&nbsp;</div>
		    <div class="layui-form" id="addInfo_form">
		       	<div class="layui-form-item">
		       		<div class="layui-inline">
			      		<label class="layui-form-label">模板名称</label>
			      		<div class="layui-input-inline">
			        		<input type="text" name="sname" value="${template.sname }" maxlength="50" lay-verify="required" placeholder="请输入模板名称" autocomplete="off" class="layui-input">
			      		</div>
			      	</div>
		       		<div class="layui-inline">
			      		<label class="layui-form-label">对应业务表单表名</label>
			      		<div class="layui-input-inline">
			        		<input type="text" name="srcBizTable" value="${template.srcBizTable }" maxlength="40" lay-verify="required" placeholder="请输入对应业务表单表名"  autocomplete="off" class="layui-input">
			      		</div>
			      	</div>
			      	<div class="layui-inline">
			      		<label class="layui-form-label">分支标识</label>
			      		<div class="layui-input-inline">
			        		<input type="text" name="branchCode" value="${template.branchCode }" maxlength="100" placeholder="请输入分支标识（选填）" autocomplete="off" class="layui-input">
			      		</div>
			      	</div>
			    </div>
			    <div class="layui-form-item">
			   		<div class="layui-inline">
			      		<label class="layui-form-label">是否可修改</label>
			      		<div class="layui-input-inline">
				      		<select id="edit_from-bisModify" name="isEdit" lay-verify="required">
								<option value="1">可修改</option>
		    					<option value="0">不可修改</option>
							</select>
			      		</div>
			      	</div>
				    <div class="layui-inline">
			      		<label class="layui-form-label" >备忘</label>
			      		<div class="layui-input-inline">
			      			<input type="text" name="memo" lay-verify="required" value="${template.memo }" style="width:220px;" maxlength="200" placeholder="请输入备忘" autocomplete="off" class="layui-input">
			      		</div>
			      	</div>
			    </div>
			    <div style="padding-top:15px;"></div>
			    <hr>
			    <div style="padding-top:15px;"></div>
			    <fieldset class="layui-elem-field layui-field-title fieldset" style="padding-top:10px;">
			  		<legend class="layui-btn layui-legend"  style="text-align:left;cursor:default;">节点配置  ></legend>
			  		<div class="layui-form-item marginset">
			  			<div class="layui-inline" id="linkRow1"  lay-filter="linkRow1">
				  			<div class="layui-input-block layui-hide">
						    	<button type="button" class="layui-btn layui-btn-normal layui-btn-sm"  id="addLinkBtn1"  onclick="addRow(this);"><i class="layui-icon">&#xe654;</i></button>
						    </div>

					      	<div class="layui-input-block">
					      		<a class="layui-btn width122">发起人</a>
					      		<input type="hidden" value="-1" name="chooseKey1" id="chooseKey1"/>
					      		<input type="hidden" value="1" name="nodeIdx1" id="nodeIdx1" />
					      		<input type="hidden" value="发起人" name="nodeName1" id="nodeName1"/>
					      		<input type="hidden" value="1" name="nodePolicy1" id="nodePolicy1"/>
					      		<input type="hidden" value="1" name="routeFlag1" id="routeFlag1"/>
					      		<%--<i class="layui-icon layui-icon-right"></i>--%>
					      	</div>
					      	<%--<div class="layui-input-block margin-top5">
					      		<input type="text" value="" readonly="readonly" style="border:0px" autocomplete="off" class="layui-input">
					      	</div>
					      	<div class="layui-input-block margin-top5">
					      		<input type="text" value="" readonly="readonly" style="border:0px" autocomplete="off" class="layui-input">
					      	</div>
					      	<div class="layui-input-block margin-top5">
					      		<input type="text" value="" readonly="readonly" style="border:0px" autocomplete="off" class="layui-input">
					      	</div>--%>
				      	</div>

						<c:if test="${empty templateFirst.nodes }"></c:if>
						<c:forEach items="${templateFirst.nodes }" var="node" varStatus="status">
							<c:if test="${not status.first}">
								<div class="layui-inline" id="linkRow${node.nodeIdx}" lay-filter="linkRow${node.nodeIdx}">
									<i class="layui-icon layui-icon-right"></i>
									<div class="layui-inline">
										<input type="text" value="${node.nodeName}" onchange="inputchange(${node.nodeIdx},this)" name="nodeTitle${node.nodeIdx}" class="layui-input width122" style="border: none;background-color: #009688;color: #fff" onclick="stopPror(event)">
									</div>
									<div style="border:1px solid #5fb878" id="nodeDiv${node.nodeIdx}_1">
										<div class="layui-inline">
											<c:choose>
												<c:when test="${node.nodePolicy==1}">
													<input type="text" value="依此审批"  disabled class="layui-input" style="border: none;" onclick="stopPror(event)">
												</c:when>
												<c:when test="${node.nodePolicy==2}">
													<input type="text" value="知会" disabled class="layui-input" style="border: none;" onclick="stopPror(event)">
												</c:when>
												<c:when test="${node.nodePolicy==3}">
													<input type="text" value="会签" disabled class="layui-input" style="border: none;" onclick="stopPror(event)">
												</c:when>
											</c:choose>
										</div>
										<div class="layui-inline">
											<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="addLinkBtn${node.nodeIdx}" <c:if test="${node.nodePolicy!=3}">style="display: none"</c:if> onclick="addRow2(${node.nodeIdx},event);"><i class="layui-icon">&#xe654;</i></button><%--onclick="addRow(this);"--%>
											<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="delLinkBtn${node.nodeIdx}" onclick="deleteRow(${node.nodeIdx},event)"><i class="layui-icon">&#xe640;</i></button>
										</div>
										<input type="hidden" value="${node.nodeIdx}" name="nodeIdx${node.nodeIdx}_1" />
										<input type="hidden" value="${node.nodePolicy}" name="nodePolicy${node.nodeIdx}_1" />
										<input type="hidden" value="${node.nodeName}" name="nodeName${node.nodeIdx}_1">
										<div class="layui-input-block width122 margin-top5">
											<div class="layui-inline" style="width: 130px;" onclick="stopPror(event)">
												<select name="routeFlag${node.nodeIdx}_1" lay-verify="required" lay-filter="add_select_routeFlag">
													<option value="">请选择-审批角色范围</option>
													<option value="1" <c:if test="${node.routeFlag == 1}">selected</c:if>>集团总部</option>
													<option value="2" <c:if test="${node.routeFlag == 2}">selected</c:if>>区域公司</option>
													<option value="3" <c:if test="${node.routeFlag == 3}">selected</c:if>>项目公司</option>
													<option value="4" <c:if test="${node.routeFlag == 4}">selected</c:if>>部门负责人</option>
													<option value="5" <c:if test="${node.routeFlag == 5}">selected</c:if>>公司法人</option>
												</select>
											</div>
											<c:if test="${node.nodePolicy==3}">
												<div class="layui-inline">
													<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="deleteNodeDiv${node.nodeIdx}_1" onclick="deleteDiv('${node.nodeIdx}_1',event)"><i class="layui-icon">&#xe640;</i></button>
												</div>
											</c:if>
										</div>
										<div class="layui-input-block width122 margin-top5">
											<input style="display:block" type="text" id="checkUserName${node.nodeIdx}_1" value="${node.checkUserName }" value="" readonly="readonly" style="border:0px" autocomplete="off" placeholder="请选择审批角色" class="layui-input">
											<i style="display:block" class="layui-icon layui-icon-search icon-i" id="queryCheck${node.nodeIdx}_1" onclick="queryCheckUser('${node.nodeIdx}_1',0);"></i>
											<input style="display:none" type="hidden" name="chooseKey${node.nodeIdx}_1"  value="${node.chooseKey}" id="chooseKey${node.nodeIdx}_1" maxlength="50"  autocomplete="off" placeholder="请输入对应名称" class="layui-input"/>
										</div>
									</div>

									<c:forEach items="${node.childList }" var="nodeChlid" varStatus="sta">
										<div style="border:1px solid #5fb878" id="nodeDiv${nodeChlid.nodeIdx}_${sta.index+2}">
											<input type="hidden" value="${nodeChlid.nodeIdx}" name="nodeIdx${nodeChlid.nodeIdx}_${sta.index+2}" />
											<input type="hidden" value="${nodeChlid.nodePolicy}" name="nodePolicy${nodeChlid.nodeIdx}_${sta.index+2}" />
											<input type="hidden" value="${nodeChlid.nodeName}" name="nodeName${nodeChlid.nodeIdx}_${sta.index+2}">
											<div class="layui-input-block width122 margin-top5">
												<div class="layui-inline" style="width: 130px;" onclick="stopPror(event)">
													<select name="routeFlag${nodeChlid.nodeIdx}_${sta.index+2}" lay-verify="required" lay-filter="add_select_routeFlag">
														<option value="">请选择-审批角色范围</option>
														<option value="1" <c:if test="${nodeChlid.routeFlag == 1}">selected</c:if>>集团总部</option>
														<option value="2" <c:if test="${nodeChlid.routeFlag == 2}">selected</c:if>>区域公司</option>
														<option value="3" <c:if test="${nodeChlid.routeFlag == 3}">selected</c:if>>项目公司</option>
														<option value="4" <c:if test="${nodeChlid.routeFlag == 4}">selected</c:if>>部门负责人</option>
														<option value="5" <c:if test="${nodeChlid.routeFlag == 5}">selected</c:if>>公司法人</option>
													</select>
												</div>
												<div class="layui-inline">
													<button type="button" class="layui-btn layui-btn-normal layui-btn-sm"  id="deleteNodeDiv${nodeChlid.nodeIdx}_${sta.index+2}" onclick="deleteDiv('${nodeChlid.nodeIdx}_${sta.index+2}',event)"><i class="layui-icon">&#xe640;</i></button>
												</div>
											</div>
											<div class="layui-input-block width122 margin-top5">
												<input style="display:block" type="text" id="checkUserName${nodeChlid.nodeIdx}_${sta.index+2}" value="${nodeChlid.checkUserName }" readonly="readonly" style="border:0px" autocomplete="off" placeholder="请选择审批角色" class="layui-input">
												<i style="display:block" class="layui-icon layui-icon-search icon-i" id="queryCheck${nodeChlid.nodeIdx}_${sta.index+2}" onclick="queryCheckUser('${nodeChlid.nodeIdx}_${sta.index+2}',0);"></i>
												<input style="display:none" type="hidden" name="chooseKey${nodeChlid.nodeIdx}_${sta.index+2}" value="${nodeChlid.chooseKey}" id="chooseKey${nodeChlid.nodeIdx}_${sta.index+2}" maxlength="50"  autocomplete="off" placeholder="请输入对应名称" class="layui-input"/>
											</div>
										</div>
									</c:forEach>

								</div>

							</c:if>


						</c:forEach>


				      	<%--<c:if test="${empty template.nodes }"></c:if>
				      	<c:forEach items="${template.nodes }" var="node" varStatus="status">
				      		<c:if test="${not status.first}">
					  			<div class="layui-inline" id="linkRow${status.index + 1}">
						  			<div class="layui-input-block">
								    	<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="addLinkBtn${status.index + 1}" onclick="addRow(this);"><i class="layui-icon">&#xe654;</i></button>
								    	<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="delLinkBtn${status.index + 1}" onclick="deleteRow(${status.index + 1})"><i class="layui-icon">&#xe640;</i></button>
								    </div>
							      	<div class="layui-input-block">
							      		<input type="text" value="${node.nodeName }" name="nodeName${status.index + 1}" class="layui-btn width122" maxlength="50" lay-verify="required" autocomplete="off">
							      		<input type="hidden" value="${node.nodeIdx }" name="nodeIdx${status.index + 1}" />
							      		<i class="layui-icon layui-icon-right"></i>
							      	</div>
						      		<div class="layui-input-block width122 margin-top5">
							      		<select name="nodePolicy${status.index + 1}" lay-verify="required">
											<option value="">请选择-节点权限</option>
											<option value="1" <c:if test="${node.nodePolicy == 1}">selected</c:if>>审批</option>
											<option value="2" <c:if test="${node.nodePolicy == 2}">selected</c:if>>知会</option>
											<option value="3" <c:if test="${node.nodePolicy == 3}">selected</c:if>>会签</option>
										</select>
								    </div>
						      		<div class="layui-input-block width122 margin-top5">
							      		<select name="routeFlag${status.index + 1 }" lay-verify="required" lay-filter="add_select_routeFlag">
											<option value="">请选择-节点标识</option>
											<option value="1" <c:if test="${node.routeFlag == 1}">selected</c:if>>固定人员</option>
											<option value="2" <c:if test="${node.routeFlag == 2}">selected</c:if>>本部门内</option>
											<option value="3" <c:if test="${node.routeFlag == 3}">selected</c:if>>本公司内</option>
											<option value="5" <c:if test="${node.routeFlag == 5}">selected</c:if>>总部公司</option>
											<option value="4" <c:if test="${node.routeFlag == 4}">selected</c:if>>上级区域公司</option>
										</select>
								    </div>
							      	<div class="layui-input-block width122 margin-top5">
							      		<input <c:if test="${node.routeFlag != 1}">style="display:none"</c:if> type="text" id="checkUserName${status.index + 1 }" value="审批人：${node.checkUserName }" readonly="readonly" style="border:0px" autocomplete="off" placeholder="请选择审批角色" class="layui-input">
							      		<i class="layui-icon layui-icon-search icon-i" <c:if test="${node.routeFlag != 1}">style="display:none"</c:if> id="queryCheck${status.index + 1}" onclick="queryCheckUser(${status.index + 1},0);"></i>
					      				<input <c:if test="${node.routeFlag == 1}">style="display:none"</c:if> type="text" value="${node.chooseKey }" name="chooseKey${status.index + 1}" id="chooseKey${status.index + 1}" maxlength="50" <c:if test="${node.routeFlag != 1}">lay-verify="required"</c:if> autocomplete="off" placeholder="请输入对应名称" class="layui-input"/>
							      	</div>
						      	</div>
					      	</c:if>
				      	</c:forEach>--%>
				    </div>
			  	</fieldset>
				<input type="hidden" name="templateId" value="${template.id }" />
				<fieldset class="layui-elem-field layui-field-title fieldset" style="text-align:center">
					<div class="layui-form-item">
						<div class="layui-input-block">
				      		<input class="layui-btn layui-btn-normal layui-btn-lg" style="margin-top:6px;" type="button" lay-submit lay-filter="addInfo_form_submit" id="addInfo_form_submit" value="提交">
				      	</div>
				    </div>
			    </fieldset>
			</div>
		  	<!-- 分配审批人div  -->
       		<div id="w_givencheckuser" lay-filter="w_givencheckuser" class="layui-form" style="padding: 20px 30px 0 0;display:none">
	        	<div class="layui-form-item">
		          	<div class="layui-inline">
			            <label class="layui-form-label">审批角色名称</label>
			            <div class="layui-input-inline">
			              <input type="text" id="srealName" name="srealName" placeholder="请输入" autocomplete="off" class="layui-input">
			            </div>
		          	</div>
		          	<div class="layui-inline">
			            <button class="layui-btn layuiadmin-btn-admin" id="queryOperator">
			              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
			            </button>
		          	</div>
		        </div>
	         	<!-- 数据表单 -->
        		<table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
        		<div class="layui-form-item layui-hide">
	                <input type="button" lay-submit id="lay-givencheckuser-submit" lay-filter="lay-givencheckuser-submit" value="确认修改"/>
	            </div>
	        </div>
  		</div>
	</div>
</div>
  <script>
  String.prototype.format = function () {
		var model = arguments;
	    return this.replace(/\{(\d+)\}/g, function (s, i) {
	        return !!model[i]?model[i]:'';
	    });
	}
  layui.config({
	    base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
	  }).extend({
	    index: 'lib/index' //主入口模块
	  }).use(['index', 'set' , 'table','dropdown'], function(){
	    var $ = layui.$
	    ,table = layui.table
	    ,$ = layui.$
	    ,form = layui.form;

	  $("div[id^='linkRow']").each(function(){
		  var tId = $(this).attr("id").substring(7);
		  tId = parseInt(tId);
		  console.log(tId)
		  var dropdown = layui.dropdown;
		  dropdown.suite("#linkRow"+tId, {
			  align: "left",
			  menus: [{txt: "依此审批",event: 'addLinkBtn'+tId+'_1'}, {txt: "知会", event:'addLinkBtn'+tId+'_2'} , {txt: '会签', event:'addLinkBtn'+tId+'_3'}]
		  })
		  dropdown.onFilter("linkRow"+tId, function (event) {
			  console.log(event);
			  var th=event.split('_')[0];
			  var nodePolicy=event.split('_')[1];
			  var thisid = th.substring(10);
			  var nodeName="";
			  var num=1;
			  if(nodePolicy=='1')nodeName="依此审批";
			  if(nodePolicy=='2')nodeName="知会";
			  if(nodePolicy=='3'){nodeName="会签";num=2;};
			  addRow1(th,nodePolicy,nodeName,num);
		  });
	  });


	    //监听行单击事件然后选中行
	    /*table.on('row(LAY-user-back-manage)', function(obj){
		    var tr = obj.tr;
		    tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');//选中行样式
            tr.find('input[lay-type="layTableRadio"]').prop("checked",true);
		    layui.form.render();
	    });*/
	    //select节点标识监听
	    /*form.on('select(add_select_routeFlag)', function(data) {
	    	console.log(data)
	    	var routeFlag = data.value;
	    	//获取编号
	    	var num = data.elem.name.substr(9);
	    	$("#chooseKey"+num).val('');
	    	$("#checkUserName"+num).val('');
	    	if(routeFlag == '1'){
	    		$("#checkUserName"+num).show();
	    		$("#queryCheck"+num).show();
	    		$("#chooseKey"+num).hide();
	    		$("#chooseKey"+num).removeAttr("lay-verify");
	    	}else{
	    		$("#checkUserName"+num).hide();
	    		$("#queryCheck"+num).hide();
	    		$("#chooseKey"+num).show();
	    		$("#chooseKey"+num).attr("lay-verify","required");
	    	}
		});*/
	    $('#edit_from-bisModify').val('${template.isEdit}');
	    form.render();

	  	//监听提交
	  	form.on('submit(addInfo_form_submit)', function(data){
	  		var flag = true;
	  		$("input[id^='chooseKey']").each(function(){
				if(this.value == ''){
					layer.msg('审批角色未选择',{icon: 7});
					flag = false;
					return false;
				}
			});
	  		if(flag){
	  			var field = data.field; //获取提交的字段
		  		layer.load();
		  		$("#addInfo_form_submit").removeAttr("lay-submit").removeClass("layui-btn-normal").addClass("layui-btn-disabled");
		  		console.log(field);
		       	//提交 Ajax 成功后，静态更新表格中的数据
		       	$.ajax({
		           	type : 'post',
					url : basePath + '/flow/addNode',
		           	data:  field,
					success:function(result){
						if(result.success){
							layer.closeAll('loading'); //关闭loading
							layer.msg('提交成功~',{icon: 1});
							//三秒后关闭当前页面
							setTimeout(function(){
								// 刷新首页
								var iframe = window.parent.layui.admin.tabsBody(0).find(".layadmin-iframe");
								iframe[0].contentWindow.location.reload(true);
								top.layui.admin.closeThisTabs(); // 关闭当前标签页
						        }, 3000);
						}else{
							layer.closeAll('loading'); //关闭loading
		  					$("#addInfo_form_submit").removeClass("layui-btn-disabled").addClass("layui-btn-normal").attr("lay-submit","");
							layer.msg(result.msg,{icon: 7});
					  	}
					}
		       	});
	  		}
	   	});

	  });
  	function queryCheckUser(inx,flag){
  		var $ = layui.$
  		,table = layui.table
	    ,form = layui.form;
  		$("#queryOperator").attr("onclick","queryCheckUser("+inx+",1)");
  		// 表单
  	    table.render({
  	    	id: 'userList'
  	      ,elem: '#LAY-user-back-manage'
  	      ,where: {'name':$('#srealName').val(),'type':$("select[name=routeFlag" + inx + "]").val()}
  	      ,url: basePath + '/postGroup/queryDataByCondition' //请求数据接口
  	      ,cols: [[
  	        {type: 'radio', fixed: 'left'}
  	        ,{field: 'name', title: '审批角色名称'}
  	        ,{field: 'type', title: '审批角色范围',templet:function (d) {
  	        	if(d.type=='1'){return '集团总部';}
				if(d.type=='2'){return '区域公司';}
				if(d.type=='3'){return '项目公司';}
				if(d.type=='4'){return '部门负责人';}
				if(d.type=='5'){return '公司法人';}
					}}
  	        ,{field: 'roleType', title: '审批角色类型', templet: function(d){
						if(d.roleType=='1'){return '岗位';}
						if(d.roleType=='2'){return '固定人员';}
  	        	}}

  	      ]]
  	      ,page: true
  	      ,limit: 10
  	    });

  	  // 监听行双击事件
  	  table.on('rowDouble(LAY-user-back-manage)',
	  	  function(obj) {
			  	$("#checkUserName"+inx).val(obj.data.name);
			  	$("#chooseKey"+inx).val(obj.data.id);
				layui.layer.closeAll();
  	  });

  	 	if(flag == 0){
  			layui.layer.open({
       	 		title: '分配审批角色组[双击行可选中]'
           		,type: 1
           		,shadeClose: false
           		//,btn: ['确定', '取消']
           		,btnAlign: 'c'
           		,area: layui.admin.screen() < 2 ? ['80%', '550px'] : ['880px', '550px']
           		,content: $('#w_givencheckuser')
           		,yes: function(index, layero){
         	  		var submitID = 'lay-givencheckuser-submit'
         		  	,submit = layero.contents().find('#'+ submitID);
       	  			// 确认选择
		    		form.on('submit('+ submitID +')', function(data){
		    			var checkStatus = layui.table.checkStatus('userList');
		    			if(checkStatus.data.length > 0){//当前是否选中状态
		    				var data = checkStatus.data;
		    			  	$("#checkUserName"+inx).val(data[0].name);
		    			  	$("#chooseKey"+inx).val(data[0].id);
		    				layui.layer.close(index);
		    			}else{
		    				layui.layer.msg("请先选中一个审批角色组",{icon: 7});
		    			}
	  		    	});
		    		submit.trigger('click');
           		}
         	});
		}
  	}

	//显示审核信息
	function showCheckInfo(obj){
		var $ = layui.$;
		var showHtml = $(obj).next().html();
		if(showHtml == '' || showHtml == null){
			return;
		}
		layui.layer.tips(showHtml, obj, {
		 	tips: 3,
			time: 0 //5秒关闭（如果不配置，默认是3秒）
		});
	}
	//关闭审核信息tip
	function closeCheckInfo(){
		layer.closeAll('tips'); //关闭所有的tips层
	}

	//新增一行
	function addRow(t){
		var $ = layui.$;
		var thisid = $(t).attr("id").substring(10);
		console.log(thisid)
		thisid = $("div[id=linkRow" + thisid + "]").find("input[name^='nodeIdx']").val();
		console.log(thisid)
		thisid = parseInt(thisid);
		var nextIndex = parseInt(thisid) + 1;
		var maxInd = $("div[id^='linkRow']").length;
		$("div[id^='linkRow']").each(function(){
			var tId = $(this).attr("id").substring(7);
			tId = parseInt(tId);
		  	if(tId > maxInd){
		  		maxInd = parseInt(tId);
		  	}
		  	var thisIndex = $(this).find("input[name^='nodeIdx']").val();
		  	thisIndex = parseInt(thisIndex);
		  	var nextId = thisIndex + 1;
		  	if(thisIndex > thisid) {
		  		var $linkRow = $("div[id=linkRow" + tId + "]");
		  		$linkRow.find("button[id^='addLinkBtn']").attr("id","addLinkBtn"+nextId);
		  		$linkRow.find("button[id^='delLinkBtn']").attr("id","delLinkBtn"+nextId);
		  		$linkRow.find("button[id^='delLinkBtn']").attr("onclick","deleteRow("+nextId+",event)");
		  		$linkRow.find("input[name^='nodeName']").attr("name","nodeName"+nextId);
		  		$linkRow.find("input[id^='checkUserName']").attr("id","checkUserName"+nextId);
		  		$linkRow.find("i[id^='queryCheck']").attr("id","queryCheck"+nextId);
		  		$linkRow.find("i[id^='queryCheck']").attr("onclick","queryCheckUser("+nextId+",0)");
		  		$linkRow.find("input[name^='chooseKey']").attr("name","chooseKey"+nextId);
		  		$linkRow.find("input[name^='chooseKey']").attr("id","chooseKey"+nextId);
		  		$linkRow.find("input[name^='nodeIdx']").attr("name","nodeIdx"+nextId);
		  		$linkRow.find("input[name^='nodeIdx']").val(nextId);
		  		$linkRow.find("select[name^='routeFlag']").attr("name","routeFlag"+nextId);
		  		$linkRow.find("select[name^='nodePolicy']").attr("name","nodePolicy"+nextId);
		  	}
		});
		$("div[id^='linkRow']").each(function(){
			var tId = $(this).attr("id").substring(7);
			tId = parseInt(tId);
		  	var thisIndex = $(this).find("input[name^='nodeIdx']").val();
		  	thisIndex = parseInt(thisIndex);
		  	if(tId != thisIndex){
		  		$(this).attr("id","linkRow"+thisIndex);
		  	}
		});
		var ind = maxInd + 1;
		var layuiRowHtml = $('#layui-row').html();
		var tempHtml = layuiRowHtml.format(nextIndex);
		$(t).parent().parent().after(tempHtml);
		layui.form.render();
	}
  function addRow1(id,nodePolicy,nodeName,num){
	  var $ = layui.$;
	  var thisid = id.substring(10);
	  thisid = $("div[id=linkRow" + thisid + "]").find("input[name^='nodeIdx']").val();
	  thisid = parseInt(thisid);
	  var nextIndex = parseInt(thisid) + 1;
	  var maxInd = $("div[id^='linkRow']").length;
	  $("div[id^='linkRow']").each(function(){
		  var tId = $(this).attr("id").substring(7);

		  tId = parseInt(tId);
		  if(tId > maxInd){
			  maxInd = parseInt(tId);
		  }
		  var thisIndex = $(this).find("input[name^='nodeIdx']").val();
		  thisIndex = parseInt(thisIndex);
		  var nextId = thisIndex + 1;
		  if(thisIndex > thisid) {
			  var $linkRow = $("div[id=linkRow" + tId + "]");
			  var nodeDivlength =$linkRow.find("div[id^='nodeDiv']").length;
			  //alert(nodeDivlength)
			  $linkRow.find("button[id^='addLinkBtn']").attr("id","addLinkBtn"+nextId);
			  $linkRow.find("button[id^='addLinkBtn']").attr("onclick","addRow2("+nextId+",event)");
			  $linkRow.find("button[id^='delLinkBtn']").attr("id","delLinkBtn"+nextId);
			  $linkRow.find("button[id^='delLinkBtn']").attr("onclick","deleteRow("+nextId+",event)");
			  //$linkRow.find("input[name^='nodeName']").attr("name","nodeName"+nextId);
			  $linkRow.find("input[name^='nodeName']").each(function (idx) {
			  		$(this).attr("name","nodeName"+nextId+"_"+(idx+1));
			  });
			  //$linkRow.find("input[id^='checkUserName']").attr("id","checkUserName"+nextId);
			  $linkRow.find("input[id^='checkUserName']").each(function (idx) {
				  $(this).attr("name","checkUserName"+nextId+"_"+(idx+1));
				  $(this).attr("id","checkUserName"+nextId+"_"+(idx+1));
			  });
			  //$linkRow.find("i[id^='queryCheck']").attr("id","queryCheck"+nextId);
			  //$linkRow.find("i[id^='queryCheck']").attr("onclick","queryCheckUser("+nextId+",0)");
			  $linkRow.find("i[id^='queryCheck']").each(function (idx) {
				  var ind=idx+1;
				  $(this).attr("id","queryCheck"+nextId+"_"+ind);
				  $(this).attr("onclick","queryCheckUser('"+nextId+"_"+ind+"',0)");
			  });
			  //$linkRow.find("input[name^='chooseKey']").attr("name","chooseKey"+nextId);
			  //$linkRow.find("input[name^='chooseKey']").attr("id","chooseKey"+nextId);
			  $linkRow.find("input[name^='chooseKey']").each(function (idx) {
				  $(this).attr("id","chooseKey"+nextId+"_"+(idx+1));
				  $(this).attr("name","chooseKey"+nextId+"_"+(idx+1));
			  });
			  //$linkRow.find("input[name^='nodeIdx']").attr("name","nodeIdx"+nextId);
			  //$linkRow.find("input[name^='nodeIdx']").val(nextId);
			  $linkRow.find("input[name^='nodeIdx']").each(function (idx) {
				  $(this).attr("name","nodeIdx"+nextId+"_"+(idx+1));
				  $(this).val(nextId);
			  });
			  //$linkRow.find("select[name^='routeFlag']").attr("name","routeFlag"+nextId);
			  $linkRow.find("select[name^='routeFlag']").each(function (idx) {
				  $(this).attr("name","routeFlag"+nextId+"_"+(idx+1));
			  });
			  //$linkRow.find("select[name^='nodePolicy']").attr("name","nodePolicy"+nextId);
			  $linkRow.find("input[name^='nodePolicy']").each(function (idx) {
				  $(this).attr("name","nodePolicy"+nextId+"_"+(idx+1));
			  });
			  $linkRow.find("button[id^='deleteNodeDiv']").each(function (idx) {
				  var ind=idx+1;
				  $(this).attr("id","deleteNodeDiv"+nextId+"_"+ind);
				  $(this).attr("onclick","deleteDiv('"+nextId+"_"+ind+"',event)");
			  });
			  $linkRow.find("input[name^='nodeTitle']").attr("name","nodeTitle"+nextId);
			  $linkRow.find("div[id^='nodeDiv']").each(function (idx) {
				  $(this).attr("id","nodeDiv"+nextId+"_"+(idx+1));
			  });
		  }
	  });
	  $("div[id^='linkRow']").each(function(){
		  var tId = $(this).attr("id").substring(7);
		  tId = parseInt(tId);
		  var thisIndex = $(this).find("input[name^='nodeIdx']").val();
		  thisIndex = parseInt(thisIndex);
		  if(tId != thisIndex){
			  $(this).attr("id","linkRow"+thisIndex);
			  $(this).attr("lay-filter","linkRow"+thisIndex);
		  }
	  });
	  var ind = maxInd + 1;
	  var layuiRowHtml = $('#layui-row').html();
	  var tempHtml = layuiRowHtml.format(nextIndex);
	  $("#"+id).parent().parent().after(tempHtml);

	  if(num>1){
		  var nodeDiv = $('#node_div').html();
		  var nodeHtml=nodeDiv.format(nextIndex,num);
		  $("#nodeDiv"+nextIndex+"_"+(num-1)).after(nodeHtml);
		  $("div[id=linkRow" + nextIndex + "]").find("button[id^='addLinkBtn']").show();
	  }else {
		  $("div[id=linkRow" + nextIndex + "]").find("button[id^='deleteNodeDiv']").hide();
	  }
	  $("div[id=linkRow" + nextIndex + "]").find("input[name^='nodePolicy']").each(function (idx) {
		  $(this).val(nodePolicy);
	  });
	  $("div[id=linkRow" + nextIndex + "]").find("input[name^='nodePolicyName']").val(nodeName);
	  /*$("div[id=linkRow" + nextIndex + "]").find("input[name^='nodeName']").each(function (idx) {
		  $(this).val(nodeName);
	  });*/

	  $("div[id^='linkRow']").each(function(){
		  var tId = $(this).attr("id").substring(7);
		  tId = parseInt(tId);
		  var dropdown = layui.dropdown;
		  dropdown.suite("#linkRow"+tId, {
			  align: "left",
			  menus: [{txt: "依此审批",event: 'addLinkBtn'+tId+'_1'}, {txt: "知会", event:'addLinkBtn'+tId+'_2'} , {txt: '会签', event:'addLinkBtn'+tId+'_3'}]
		  })
		  dropdown.onFilter("linkRow"+tId, function (event) {
			  var th=event.split('_')[0];
			  var nodePolicy=event.split('_')[1];
			  var thisid = th.substring(10);
			  var nodeName="";
			  var num=1;
			  if(nodePolicy=='1')nodeName="依此审批";
			  if(nodePolicy=='2')nodeName="知会";
			  if(nodePolicy=='3'){nodeName="会签";num=2;};
			  addRow1(th,nodePolicy,nodeName,num);
		  });
	  });
	  layui.form.render();
  }
  function addRow2(t,e) {
	  if(e){
		  e.stopPropagation();
	  }
	  var $ = layui.$;
	  var num=$("div[id=linkRow" + t + "]").find("div[id^='nodeDiv']").length;
	  var nodeDiv = $('#node_div').html();
	  var nodeHtml=nodeDiv.format(t,num+1);
	  $("#nodeDiv"+t+"_"+(num)).after(nodeHtml);
	  $("div[id=linkRow" + t + "]").find("button[id^='addLinkBtn']").show();

	  $("div[id=linkRow" + t + "]").find("input[name='nodePolicy" + t+"_"+(num+1) + "']").each(function (idx) {
		  $(this).val(3);
	  });
	  var nodeName=$("div[id=linkRow" + t + "]").find("input[name^='nodeTitle']").val();
	  $("div[id=linkRow" + t + "]").find("input[name='nodeName" + t +"_"+(num+1)+ "']").each(function (idx) {
		  $(this).val(nodeName);
	  });
	  layui.form.render();
  }

  function inputchange(index,t){
	  var $ = layui.$;
	  $("div[id=linkRow" + index + "]").find("input[name^='nodeName']").each(function (idx) {
		  $(this).val($(t).val());
	  });
  }

	//删除一行
	function deleteRow(id,e){
  		if(e){
			e.stopPropagation();
		}
		var $ = layui.$;
		layui.layer.confirm('确定删除？',{icon:3, title :'提示'}, function(index){
 		    var rowLength = $("div[id^='linkRow']").length;
 		    if(rowLength < 2) {
	    		layui.layer.alert("至少保留一个联系人！", {icon:0});
	    		return;
	    	}
 		   	var thisid = $("div[id=linkRow" + id + "]").find("input[name^='nodeIdx']").val();
	    	thisid = parseInt(thisid);
	    	$("div[id^='linkRow']").each(function(){
				var tId = $(this).attr("id").substring(7);
				tId = parseInt(tId);
				var $linkRow = $("div[id=linkRow" + tId + "]");
			  	var thisIndex = $linkRow.find("input[name^='nodeIdx']").val();
			  	thisIndex = parseInt(thisIndex);
			  	var nextId = thisIndex - 1;
			  	if(thisIndex > thisid) {
			  		$linkRow.find("button[id^='addLinkBtn']").attr("id","addLinkBtn"+nextId);
					$linkRow.find("button[id^='addLinkBtn']").attr("onclick","addRow2("+nextId+",event)");
			  		$linkRow.find("button[id^='delLinkBtn']").attr("id","delLinkBtn"+nextId);
			  		$linkRow.find("button[id^='delLinkBtn']").attr("onclick","deleteRow("+nextId+",event)");

					$linkRow.find("input[name^='nodeName']").each(function (idx) {
						$(this).attr("name","nodeName"+nextId+"_"+(idx+1));
					});
					$linkRow.find("input[id^='checkUserName']").each(function (idx) {
						$(this).attr("name","checkUserName"+nextId+"_"+(idx+1));
						$(this).attr("id","checkUserName"+nextId+"_"+(idx+1));
					});
					$linkRow.find("i[id^='queryCheck']").each(function (idx) {
						var ind=idx+1;
						$(this).attr("id","queryCheck"+nextId+"_"+ind);
						$(this).attr("onclick","queryCheckUser('"+nextId+"_"+ind+"',0)");
					});
					$linkRow.find("input[name^='chooseKey']").each(function (idx) {
						$(this).attr("id","chooseKey"+nextId+"_"+(idx+1));
						$(this).attr("name","chooseKey"+nextId+"_"+(idx+1));
					});
					$linkRow.find("input[name^='nodeIdx']").each(function (idx) {
						$(this).attr("name","nodeIdx"+nextId+"_"+(idx+1));
						$(this).val(nextId);
					});
					$linkRow.find("select[name^='routeFlag']").each(function (idx) {
						$(this).attr("name","routeFlag"+nextId+"_"+(idx+1));
					});
					$linkRow.find("input[name^='nodePolicy']").each(function (idx) {
						$(this).attr("name","nodePolicy"+nextId+"_"+(idx+1));
					});
					$linkRow.find("button[id^='deleteNodeDiv']").each(function (idx) {
						var ind=idx+1;
						$(this).attr("id","deleteNodeDiv"+nextId+"_"+ind);
						$(this).attr("onclick","deleteDiv('"+nextId+"_"+ind+"',event)");
					});
					$linkRow.find("input[name^='nodeTitle']").attr("name","nodeTitle"+nextId);
					$linkRow.find("div[id^='nodeDiv']").each(function (idx) {
						$(this).attr("id","nodeDiv"+nextId+"_"+(idx+1));
					});
			  	}
			});
	    	$("#linkRow"+id).remove();
	    	$("div[id^='linkRow']").each(function(){
				var tId = $(this).attr("id").substring(7);
				tId = parseInt(tId);
			  	var thisIndex = $(this).find("input[name^='nodeIdx']").val();
			  	thisIndex = parseInt(thisIndex);
			  	if(tId != thisIndex){
			  		$(this).attr("id","linkRow"+thisIndex);
					$(this).attr("lay-filter","linkRow"+thisIndex);
			  	}
			});
			$("div[id^='linkRow']").each(function(){
				var tId = $(this).attr("id").substring(7);
				tId = parseInt(tId);
				var dropdown = layui.dropdown;
				dropdown.suite("#linkRow"+tId, {
					align: "left",
					menus: [{txt: "依此审批",event: 'addLinkBtn'+tId+'_1'}, {txt: "知会", event:'addLinkBtn'+tId+'_2'} , {txt: '会签', event:'addLinkBtn'+tId+'_3'}]
				})
				dropdown.onFilter("linkRow"+tId, function (event) {
					var th=event.split('_')[0];
					var nodePolicy=event.split('_')[1];
					var thisid = th.substring(10);
					var nodeName="";
					var num=1;
					if(nodePolicy=='1')nodeName="依此审批";
					if(nodePolicy=='2')nodeName="知会";
					if(nodePolicy=='3'){nodeName="会签";num=2;};
					addRow1(th,nodePolicy,nodeName,num);
				});
			});
			layui.layer.close(index);
 		});
	}

	function deleteDiv(id,e) {
		if(e){
			e.stopPropagation();
		}
		var $ = layui.$;
		console.log("delete:"+"#nodeDiv"+id)
		$("#nodeDiv"+id).remove();
	}

	function stopPror(e) {
  		console.info("stop",e);
  		e.stopPropagation();
	}
  </script>
</body>
</html>

